// Tools to create common embed things
import { APIEmbed, EmbedBuilder, EmbedData } from 'discord.js';
import Bot from './bot';
import { isDev } from '../util/args';

/** Embed Wrapper */
export class Embed extends EmbedBuilder {
  /** Calls constructor */
  public static create() {
    return new Embed();
  }
  public constructor(data?: EmbedData | APIEmbed | undefined) {
    super(data);
    this.setTimestamp(Date.now())
      .setColor('#8849C0')
      .setFooter({
        text: `Made with <3 by ${Bot.authorsAsStr}`,
      });
  }
  /** Turn the embed into an error embed */
  public error(error?: any) {
    if (error) {
      if (error instanceof Error) {
        this.setTitle(`Error: ${error.name}`);
        this.setDescription(`${error.message}`);
        if (isDev)
          this.addFields({
            name: 'Stack Trace',
            value: `\`\`\`log
${error.stack?.replace(/```/giu, '`​`​`')}
\`\`\``,
          });
      } else {
        this.setDescription(`${error}`);
      }
    }
    if (!this.data.title) this.setTitle('Error');
    return this.setColor('#ff9999');
  }
}
